const events = require("events");
const EventEmitter = events.EventEmitter;

function Dog(dogName, energy) {
    this.dogName = dogName;
    this.energy = energy;
    EventEmitter.call(this);//执行EventEmitter,调用主体是this

    //this指代实例化对象
    this.on("bark", () => {

        if (this.energy > 0) {
            console.log(this.dogName + " is barking,rnergy is" + this.energy);
        }
        else {
            process.exit();
            //退出来
        }
        this.energy--;
    })

}
Dog.prototype = EventEmitter.prototype;
let dog = new Dog("taidy", 5);
let intervalId = setInterval(() => {
    dog.emit("bark");
}, 1000)
//监听
// dog.on("bark", () => {
//     console.log("dog is barking");
// })
// dog.emit("bark");
console.log(dog);